package com.eco.fanliapp.fengou;

import com.alibaba.fastjson.JSON;
import com.eco.fanliapp.utils.DtkHttpUtils;
import com.eco.fanliapp.utils.SignMD5Util;
import com.eco.fanliapp.vo.DaoTaoKeSuperCategoryInfo;
import com.eco.fanliapp.vo.respone.DTKResponse;
import com.eco.fanliapp.vo.respone.FenGouResponse;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.TreeMap;

@Service
public class FenGouItemInfoService {

    public static void main(String[] args) {
//        TreeMap<String, String> paraMap = new TreeMap<>();
//        paraMap.put("app_key", "2000143573");
//        paraMap.put("q", "手机");
//        //fengou.taobao.detail
//        //fengou.taobao.search
//        paraMap.put("method", "fengou.taobao.detail");
//        paraMap.put("t", "" + new Date().getTime());
//        paraMap.put("item_id", "608425776393");
//        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, "ZbzT#OA3q7MK-z0lvtsvnqJ1gfsiwL"));
//        FenGouResponse info = null;
//        String result = null;
//        try {
//            result = DtkHttpUtils.sendGet("http://www.jimucc.com/openapi/route", paraMap);
//            String a = decodeUnicode(result);
//            info = JSON.parseObject(a, FenGouResponse.class);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("app_key", FenGouConfig.FenGou_APPKEY);
        paraMap.put("q", "手机");
        paraMap.put("method", FenGouConfig.FenGou_SUGGEST_METHOD);
        paraMap.put("t", "" + new Date().getTime());
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, FenGouConfig.FenGou_APPSECRET));
        String result = null;
        try {
            result = DtkHttpUtils.sendGet(FenGouConfig.FenGou_URL, paraMap);
            result = decodeUnicode(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 查询商品详情
     *
     * @param t
     * @return
     */
    public FenGouResponse getItemInfo(String t, String itemId) {
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("app_key", FenGouConfig.FenGou_APPKEY);
        paraMap.put("method", FenGouConfig.FenGou_DETAIL_METHOD);
        paraMap.put("t", t);
        paraMap.put("item_id", itemId);
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, FenGouConfig.FenGou_APPSECRET));
        FenGouResponse info = null;
        String result = null;
        try {
            result = DtkHttpUtils.sendGet("http://www.jimucc.com/openapi/route", paraMap);
            String a = decodeUnicode(result);
            info = JSON.parseObject(a, FenGouResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 商品搜索
     *
     * @param q
     * @return
     */
    public String searchItemInfo(String q, String t, String itemId) {
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("app_key", FenGouConfig.FenGou_APPKEY);
        paraMap.put("q", q);
        paraMap.put("method", FenGouConfig.FenGou_SEARCH_METHOD);
        paraMap.put("t", t);
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, FenGouConfig.FenGou_APPSECRET));
        String result = null;
        try {
            result = DtkHttpUtils.sendGet(FenGouConfig.FenGou_URL, paraMap);
            result = decodeUnicode(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 商品搜索建议
     *
     * @param q
     * @return
     */
    public String getGoodsSuggest(String q, String t) {
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("app_key", FenGouConfig.FenGou_APPKEY);
        paraMap.put("q", q);
        paraMap.put("method", FenGouConfig.FenGou_SUGGEST_METHOD);
        paraMap.put("t", t);
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, FenGouConfig.FenGou_APPSECRET));
        String result = null;
        try {
            result = DtkHttpUtils.sendGet(FenGouConfig.FenGou_URL, paraMap);
            result = decodeUnicode(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String decodeUnicode(String theString) {
        char aChar;
        int len = theString.length();
        StringBuffer outBuffer = new StringBuffer(len);
        for (int x = 0; x < len; ) {
            aChar = theString.charAt(x++);
            if (aChar == '\\') {
                aChar = theString.charAt(x++);
                if (aChar == 'u') {
                    // Read the xxxx
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = theString.charAt(x++);
                        switch (aChar) {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException(
                                        "Malformed   \\uxxxx   encoding.");
                        }

                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't')
                        aChar = '\t';
                    else if (aChar == 'r')
                        aChar = '\r';
                    else if (aChar == 'n')
                        aChar = '\n';
                    else if (aChar == 'f')
                        aChar = '\f';
                    outBuffer.append(aChar);
                }
            } else
                outBuffer.append(aChar);
        }
        return outBuffer.toString();
    }
}
